
package com.craterhill.java.ems.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.PreparedStatementCreator;

import com.craterhill.java.ems.common.beans.Position;
import com.craterhill.java.ems.dao.impl.AbstractDao;
import com.craterhill.java.ems.dao.utils.EMSResultSetExtractor;

public interface PositionDao extends EMSDao<Position> {

	public final String FIELD_LIST = "obj.position_id, obj.position_name, obj.abbriviation, obj.from_date, obj.to_date, obj.position_description"
			+ AbstractDao.MONITER_FIELDS;

	public final String FIND_ALL_POSITION = "select " + FIELD_LIST + " from position obj";
	public final String FIND_POSITION = "select " + FIELD_LIST + " from position obj where obj.position_id = ?";

	public final String SAVE_POSITION = "insert into position (position_name, abbriviation, from_date, to_date, position_description, created_by) values (?, ?, ?, ?, ?, ?)";
	public final String UPDATE_POSITION = "update position set position_name = ?, abbriviation = ?, from_date = ?, to_date = ?, position_description = ?, modified_date = ? where position_id = ?";
	public final String DELETE_POSITION = "delete from position where position_id = ?";

	public final class PositionExtractor extends EMSResultSetExtractor<Position> {

		@Override
		protected Position extractResultSetData(ResultSet resultSet) throws SQLException {
			Position position = new Position();
			position.setPositionId(resultSet.getInt(1));
			position.setPositionName(resultSet.getString(2));
			position.setAbbriviation(resultSet.getString(3));
			position.setFormDate(resultSet.getDate(4));
			position.setToDate(resultSet.getDate(5));
			position.setPositionDescription(resultSet.getString(6));
			position.setStatus(resultSet.getBoolean(7));
			position.setCreatedBy(resultSet.getInt(8));
			position.setCreatedDate(resultSet.getDate(9));
			position.setModifiedDate(resultSet.getDate(10));
			return position;
		}
	}

	public final class PositionPreparedStatementCreator implements PreparedStatementCreator {

		private final int ACTION;
		private final Position position;

		public PositionPreparedStatementCreator(final int ACTION, final Position position) {
			this.ACTION = ACTION;
			this.position = position;
		}

		@Override
		public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
			PreparedStatement preparedStatement = connection.prepareStatement(this.ACTION == EMSDao.ACTION_SAVE ? SAVE_POSITION : UPDATE_POSITION);
			preparedStatement.setString(1, position.getPositionName());
			preparedStatement.setString(2, position.getAbbriviation());
			preparedStatement.setDate(3, position.getFormDate());
			preparedStatement.setDate(4, position.getToDate());
			preparedStatement.setString(5, position.getPositionDescription());

			if(this.ACTION == EMSDao.ACTION_SAVE){
				preparedStatement.setInt(6, position.getCreatedBy());
			} else{
				preparedStatement.setDate(6, position.getModifiedDate());
				preparedStatement.setInt(7, position.getPositionId());
			}

			return preparedStatement;
		}
	}
}
